package com.wuxuan.javabase_homework.homework3;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 用户界面
 */
public class UserScreen {
    private static Scanner sc = new Scanner(System.in);
    private People user;
    //所有店铺
    private List<Shop> shops;

    public UserScreen(People user) {
        this.user = user;
        //获取所有店铺，初始化在这个界面中
        this.shops = MainScreen.shops;
    }


    /**
     * 用户登录
     */
    public void menu() {
        while (true) {
            System.out.println("======================当前用户信息========================");
            System.out.println("用户ID:" + user.getId());
            System.out.println("用户名:" + user.getName());
            System.out.println("余额:" + user.getBalance());
            System.out.println("======================用户界面========================");
            System.out.println("0.退出登录");
            System.out.println("1.充值");
            System.out.println("2.查看所有店铺");
            System.out.println("3.进入店铺");
            int choose = sc.nextInt();
            switch (choose) {
                case 0:
                    MainScreen.menu();
                    return;
                case 1:
                    recharge();
                    break;
                case 2:
                    selectAllShops();
                    break;
                case 3:
                    goToShop();
                    break;
                default:
                    System.out.println("请输入正确的序号！");
            }
        }
    }

    /**
     * 充值
     */
    private void recharge() {
        System.out.println("======================充值界面========================");
        while (true) {
            System.out.println("请输入您要充值的金额：");
            double recharge = sc.nextDouble();
            if (recharge >= 0 && recharge <= 1000000) {
                user.setBalance(user.getBalance() + recharge);
                break;
            } else {
                System.out.println("金额异常！请重新输入！");
            }
        }
    }

    /**
     * 查看所有店铺
     */
    private void selectAllShops() {
        System.out.println("======================查看所有店铺========================");
        if (shops.size() > 0) {
            for (Shop shop : shops) {
                System.out.println(shop);
            }
        } else {
            System.out.println("当前平台还没有任何店铺噢~");
        }
    }

    /**
     * 进入店铺
     */
    private void goToShop() {
        System.out.println("======================进入店铺========================");
        if (shops.size() > 0) {

            while (true) {
                System.out.println("请输入店铺名：");
                String name = sc.next();

                boolean exists = false;

                if (!shops.isEmpty()) {
                    // 检查店铺名是否已存在
                    exists = shops.stream().anyMatch(p -> p.getShopName().equals(name));
                }

                if (exists) {
                    //根据店铺名获取店铺
                    Shop s = MainScreen.shops.stream().filter(z -> z.getShopName().equals(name)).findFirst().get();
                    //进入店铺界面
                    ShopScreen shopScreen = new ShopScreen(s, user);
                    shopScreen.start();
                    return;
                } else {
                    System.out.println("店铺名不存在，请重新输入！");
                }
            }

        } else {
            System.out.println("当前平台还没有任何店铺噢~");
        }

    }


}
